package com.mining.app.zxing.decoding;


import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.mining.app.zxing.camera.IMECameraManager;
import com.mining.app.zxing.view.IMEViewfinderResultPointCallback;
import qs.openxt.libs.R;
import qs.openxt.libs.qrcode.QrcodeCaptureActivity;

public final class IMECaptureActivityHandler extends Handler {
	private final QrcodeCaptureActivity activity;
	private final IMEDecodeThread decodeThread;
	private State state;

	private enum State {
		PREVIEW, SUCCESS, DONE
	}

	public IMECaptureActivityHandler(QrcodeCaptureActivity activity,
			Vector<BarcodeFormat> decodeFormats, String characterSet) {
		this.activity = activity;
		decodeThread = new IMEDecodeThread(activity, decodeFormats, characterSet,
				new IMEViewfinderResultPointCallback(activity.getViewfinderView()));
		decodeThread.start();
		state = State.SUCCESS;
		// Start ourselves capturing previews and decoding.
		IMECameraManager.get().startPreview();
		restartPreviewAndDecode();
	}

	@Override
	public void handleMessage(Message message) {
		if (message.what == R.id.qrcode_auto_focus) {
			if (state == State.PREVIEW) {
				IMECameraManager.get().requestAutoFocus(this, R.id.qrcode_auto_focus);
			}
		}
		if (message.what == R.id.qrcode_restart_preview) {
			restartPreviewAndDecode();
		} else if (message.what == R.id.qrcode_decode_succeeded) {
			state = State.SUCCESS;
			Bundle bundle = message.getData();

			/***********************************************************************/
			Bitmap barcode = bundle == null ? null : (Bitmap) bundle
					.getParcelable(IMEDecodeThread.BARCODE_BITMAP);

			activity.handleDecode((Result) message.obj, barcode);
		}
		if (message.what == R.id.qrcode_decode_failed) {
			state = State.PREVIEW;
			IMECameraManager.get().requestPreviewFrame(decodeThread.getHandler(),
					R.id.qrcode_decode);
		}
		if (message.what == R.id.qrcode_return_scan_result) {
			activity.setResult(Activity.RESULT_OK, (Intent) message.obj);
			activity.finish();
		}
		if (message.what == R.id.qrcode_launch_product_query) {
			String url = (String) message.obj;
			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
			activity.startActivity(intent);
		}
	}

	public void quitSynchronously() {
		state = State.DONE;
		IMECameraManager.get().stopPreview();
		Message quit = Message.obtain(decodeThread.getHandler(), R.id.qrcode_quit);
		quit.sendToTarget();
		try {
			decodeThread.join();
		} catch (InterruptedException e) {
			// continue
		}

		// Be absolutely sure we don't send any queued up messages
		removeMessages(R.id.qrcode_decode_succeeded);
		removeMessages(R.id.qrcode_decode_failed);
	}

	private void restartPreviewAndDecode() {
		if (state == State.SUCCESS) {
			state = State.PREVIEW;
			IMECameraManager.get().requestPreviewFrame(decodeThread.getHandler(),
					R.id.qrcode_decode);
			IMECameraManager.get().requestAutoFocus(this, R.id.qrcode_auto_focus);
			activity.drawViewfinder();
		}
	}

}
